package com.base.controller.utils.CZB;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.po.czb_sdk.GasOilprice;

import java.math.BigDecimal;

public class CZBOilUtil {

    private static final String OIL_1 = "90#";
    private static final String OIL_2 = "92#";
    private static final String OIL_3 = "95#";
    private static final String OIL_4 = "98#";
    private static final String OIL_5 = "101#";
    private static final String OIL_6 = "-40#";
    private static final String OIL_7 = "-35#";
    private static final String OIL_8 = "-30#";
    private static final String OIL_9 = "-20#";
    private static final String OIL_10 = "-10#";
    private static final String OIL_11 = "国四0#";
    private static final String OIL_12 = "0#";
    private static final String OIL_13 = "CNG";
    private static final String OIL_14 = "LNG";

    private static final String OIL_1_NUM = "90";
    private static final String OIL_2_NUM = "92";
    private static final String OIL_3_NUM = "95";
    private static final String OIL_4_NUM = "98";
    private static final String OIL_5_NUM = "101";
    private static final String OIL_6_NUM = "-40";
    private static final String OIL_7_NUM = "-35";
    private static final String OIL_8_NUM = "-30";
    private static final String OIL_9_NUM = "-20";
    private static final String OIL_10_NUM = "-10";
    private static final String OIL_11_NUM = "国四0";
    private static final String OIL_12_NUM = "0";
    private static final String OIL_13_NUM = "CNG";
    private static final String OIL_14_NUM = "LNG";

    public static String findOilNum(String num) {
        if (num.equals("OIL_1")) {
            return OIL_1;
        } else if (num.equals("OIL_2")) {
            return OIL_2;
        } else if (num.equals("OIL_3")) {
            return OIL_3;
        } else if (num.equals("OIL_4")) {
            return OIL_4;
        } else if (num.equals("OIL_5")) {
            return OIL_5;
        } else if (num.equals("OIL_6")) {
            return OIL_6;
        } else if (num.equals("OIL_7")) {
            return OIL_7;
        } else if (num.equals("OIL_8")) {
            return OIL_8;
        } else if (num.equals("OIL_9")) {
            return OIL_9;
        } else if (num.equals("OIL_10")) {
            return OIL_10;
        } else if (num.equals("OIL_11")) {
            return OIL_11;
        } else if (num.equals("OIL_12")) {
            return OIL_12;
        } else if (num.equals("OIL_13")) {
            return OIL_13;
        } else if (num.equals("OIL_14")) {
            return OIL_14;
        }
        return null;
    }

    public static String findOilNumReal(String num) {
        if (num.equals("OIL_1")) {
            return OIL_1_NUM;
        } else if (num.equals("OIL_2")) {
            return OIL_2_NUM;
        } else if (num.equals("OIL_3")) {
            return OIL_3_NUM;
        } else if (num.equals("OIL_4")) {
            return OIL_4_NUM;
        } else if (num.equals("OIL_5")) {
            return OIL_5_NUM;
        } else if (num.equals("OIL_6")) {
            return OIL_6_NUM;
        } else if (num.equals("OIL_7")) {
            return OIL_7_NUM;
        } else if (num.equals("OIL_8")) {
            return OIL_8_NUM;
        } else if (num.equals("OIL_9")) {
            return OIL_9_NUM;
        } else if (num.equals("OIL_10")) {
            return OIL_10_NUM;
        } else if (num.equals("OIL_11")) {
            return OIL_11_NUM;
        } else if (num.equals("OIL_12")) {
            return OIL_12_NUM;
        } else if (num.equals("OIL_13")) {
            return OIL_13_NUM;
        } else if (num.equals("OIL_14")) {
            return OIL_14_NUM;
        }
        return null;
    }

    public static GasOilprice getGasDetail(String mobile, String parentId, String gasIds, String oilNo, String token, Long Date) {
        GasOilprice gasOilprice = new GasOilprice();
        String re = CZBTokenUtils.getGasDetails(Date, gasIds, mobile, token);
        JSONObject jsonObject = JSONObject.parseObject(re);
        String tokenNow = null;
        //请求过期，重新调用接口
        if (jsonObject.getString("code").equals("10013")) {
            tokenNow = CZBTokenUtils.getNowToken(mobile);
            re = CZBTokenUtils.getGasDetails(Date, gasIds, mobile, tokenNow);
        }
        JSONObject js = JSONObject.parseObject(re);
        Object r1 = js.getString("result");
        JSONArray result = JSONArray.parseArray(r1.toString());
        for (Object item : result) {

            JSONObject jsonObject1 = JSONObject.parseObject(item.toString());
            gasOilprice.setPriceGasId(jsonObject1.getString("gasId"));
            JSONArray item2 = JSONArray.parseArray((jsonObject1.getString("oilPriceList")));
            for (Object l1 : item2) {
                JSONObject j1 = JSONObject.parseObject(l1.toString());
                String param1 = j1.getString("oilNo");
                if (param1.equals(oilNo)) {
                    String priceGun = j1.getString("priceGun");
                    String oilNo1 = j1.getString("oilNo");
                    String priceOfficial = j1.getString("priceOfficial");
                    String priceYfq = j1.getString("priceYfq");
                    String oilType = j1.getString("oilType");
                    String oilName = j1.getString("oilName");
                    gasOilprice.setPricegun(new BigDecimal(priceGun));
                    gasOilprice.setOilno(oilNo1);
                    gasOilprice.setOilname(oilName);
                    gasOilprice.setOiltype(oilType);
                    gasOilprice.setPriceofficial(new BigDecimal(priceOfficial));
                    gasOilprice.setPriceyfq(new BigDecimal(priceYfq));
                }
            }
        }
        return gasOilprice;
    }

    public static GasOilprice getGasDetailApi(String mobile, String parentId, String gasIds, String oilNo, String token, Long Date) {
        GasOilprice gasOilprice = new GasOilprice();
        String re = CZBTokenUtils.getGasDetailsApi(Date, gasIds, mobile, token);
        JSONObject jsonObject = JSONObject.parseObject(re);
        String tokenNow = null;
        //请求过期，重新调用接口
        if (jsonObject.getString("code").equals("10013")) {
            tokenNow = CZBTokenUtils.getNowTokenApi(mobile);
            re = CZBTokenUtils.getGasDetailsApi(Date, gasIds, mobile, tokenNow);
        }
        JSONObject js = JSONObject.parseObject(re);
        Object r1 = js.getString("result");
        JSONArray result = JSONArray.parseArray(r1.toString());
        for (Object item : result) {
            JSONObject jsonObject1 = JSONObject.parseObject(item.toString());
            gasOilprice.setPriceGasId(jsonObject1.getString("gasId"));
            JSONArray item2 = JSONArray.parseArray((jsonObject1.getString("oilPriceList")));
            for (Object l1 : item2) {
                JSONObject j1 = JSONObject.parseObject(l1.toString());
                String param1 = j1.getString("oilNo");
                if (param1.equals(oilNo)) {
                    String priceGun = j1.getString("priceGun");
                    String oilNo1 = j1.getString("oilNo");
                    String priceOfficial = j1.getString("priceOfficial");
                    String priceYfq = j1.getString("priceYfq");
                    String oilType = j1.getString("oilType");
                    String oilName = j1.getString("oilName");
                    gasOilprice.setPricegun(new BigDecimal(priceGun));
                    gasOilprice.setOilno(oilNo1);
                    gasOilprice.setOilname(oilName);
                    gasOilprice.setOiltype(oilType);
                    gasOilprice.setPriceofficial(new BigDecimal(priceOfficial));
                    gasOilprice.setPriceyfq(new BigDecimal(priceYfq));
                }
            }
        }
        return gasOilprice;
    }
}
